package com.youlai.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.Employ;
import com.youlai.system.model.form.SocialSecurityForm;
import com.youlai.system.model.query.SocialSecurityQuery;
import com.youlai.system.model.vo.SocialSecurityVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SocialSecurityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

@Tag(name = "13.社保接口")
@RestController
@RequestMapping("/api/v1/social_securitys")
@RequiredArgsConstructor
public class SocialSecurityController {
    private final SocialSecurityService socialSecurityService;

    @Operation(summary = "社保信息分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/page")
    public PageResult<SocialSecurityVO> getSocialSecurityPage(
            @ParameterObject SocialSecurityQuery queryParams
    ){

        PageInfo<SocialSecurityVO> pageInfo = socialSecurityService.getSocialSecurityList(queryParams);

        // 将PageInfo对象转换为IPage对象
        Page<SocialSecurityVO> iPage = new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
        iPage.setRecords(pageInfo.getList());
        iPage.setTotal(pageInfo.getTotal());

        return PageResult.success(iPage);
    }
    @Operation(summary = "社保全部信息分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/list")
    public PageResult<SocialSecurityVO> getSocialSecurityPageAll(
            @ParameterObject SocialSecurityQuery queryParams
    ){

        PageInfo<SocialSecurityVO> pageInfo = socialSecurityService.getSocialSecurityListAll(queryParams);

        // 将PageInfo对象转换为IPage对象
        Page<SocialSecurityVO> iPage = new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
        iPage.setRecords(pageInfo.getList());
        iPage.setTotal(pageInfo.getTotal());

        return PageResult.success(iPage);
    }

    @Operation(summary = "获取社保信息")
    @GetMapping("/{id}/VO")
    public Result<SocialSecurityVO> selectSocialSecurityAllById(
            @Parameter(description = "社保id") @PathVariable Integer id) {
        SocialSecurityVO socialSecurityVO = socialSecurityService.selectSocialSecurityAllById(id);
        return Result.success(socialSecurityVO);
    }
    @Operation(summary = "新增社保")
    @PostMapping("/save")
    @PreventDuplicateSubmit
    public Result saveSocialSecurity(
            @Valid @RequestBody SocialSecurityForm socialSecurityForm
    ) {
        Integer id = socialSecurityService.saveSocialSecurity(socialSecurityForm);
        System.out.println(id);
        return Result.success(id);
    }

    @Operation(summary = "修改社保")
    @PostMapping("/update")
    @PreventDuplicateSubmit
    public Result updateSocialSecurity(
            @Valid @RequestBody SocialSecurityForm socialSecurityForm
    ) {
        Integer id = socialSecurityService.UpdateSocialSecurity(socialSecurityForm);
        return Result.success(id);
    }
}
